import 'package:core_package/core_package.dart';
import 'package:flutter/material.dart';

class BaseNavigationHeightWidget extends StatelessWidget {
  final String? title;
  final Widget? titleWidget;
  final Widget? leftWidget;
  final List<Widget>? actions;

  final bool haveReturn;
  final Function()? returnOnTap;
  final Widget? child;
  final Color? bgColor;
  final EdgeInsets? padding;
  final bool needScrollView;
  final double navHeight;
  final bool hiddenNav;

  const BaseNavigationHeightWidget({
    Key? key,
    this.title,
    this.haveReturn = true,
    this.returnOnTap,
    this.titleWidget,
    this.child,
    this.bgColor,
    this.padding,
    this.needScrollView = true,
    this.leftWidget,
    this.actions,
    this.navHeight = 256,
    this.hiddenNav = true,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: bgColor ?? MyColors.color_246_244_239,
      body: GestureDetector(
        child: buildContent(context),
        onTap: () {
          FocusScope.of(context).requestFocus(FocusNode());
        },
      ),
    );
  }

  Widget buildContent(BuildContext context) {
    return Stack(
      children: [
        Positioned(
            height: navHeight,
            top: 0,
            left: 0,
            right: 0,
            child: Image.asset(
              MyAssetsImages.setAssetsImagesPath("icon_nav_bg1"),
              fit: BoxFit.fill,
              gaplessPlayback: true,
            )),
        Positioned(
          top: 0,
          left: 0,
          right: 0,
          child: buildNavigation(context),
        ),
        Positioned(
          top: padding != null ? padding!.top : SizeFill.statusHeight,
          left: padding != null ? padding!.left : SizeFill.setRx(10),
          bottom: 0,
          right: padding != null ? padding!.left : SizeFill.setRx(10),
          child: needScrollView ? SingleChildScrollView(child: child) : child ?? Container(),
        ),
      ],
    );
  }

  Widget buildNavigation(BuildContext context) {
    return Container(
      width: double.infinity,
      height: navHeight,
      // decoration: BoxDecoration(
      //     image: DecorationImage(
      //   image: AssetImage(
      //     MyAssetsImages.setAssetsImagesPath("icon_nav_bg1"),
      //   ),
      //   fit: BoxFit.fill,
      // )),
      padding: EdgeInsets.only(top: SizeFill.statusHeight!, left: 0, right: 0, bottom: 111 - 44.0),
      child: Container(
        height: 44,
        alignment: Alignment.center,
        child: buildNavWidget(context),
      ),
    );
  }

  Widget buildNavWidget(BuildContext context) {
    if (hiddenNav) return SizedBox();
    return Stack(
      children: [
        Positioned(
          top: 0,
          left: 0,
          bottom: 0,
          child: buildReturn(context),
        ),
        Positioned(
          top: 0,
          right: 0,
          bottom: 0,
          child: buildActions(context),
        ),
        buildTitleWidget(),
      ],
    );
  }

  Widget buildActions(BuildContext context) {
    if (actions == null || actions!.length == 0) {
      return SizedBox();
    }

    return Row(
      children: actions ?? [],
    );
  }

  Widget buildTitleWidget() {
    if (titleWidget != null || title == null) {
      return Center(child: titleWidget);
    }

    return Center(
      child: titleWidget ??
          Text(
            title ?? '',
            style: TextStyle(
              color: MyColors.formHex("#272727FF"),
              fontSize: 18,
              fontWeight: MyFontWeight.bold,
            ),
          ),
    );
  }

  Widget buildReturn(BuildContext context) {
    if (leftWidget != null) {
      return leftWidget!;
    }

    if (!haveReturn) {
      return SizedBox();
    }
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        if (returnOnTap != null) {
          returnOnTap?.call();
        } else {
          NavigatorUtil.pop(context);
        }
      },
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 20),
        alignment: Alignment.center,
        child: Image.asset(
          MyAssetsImages.setAssetsImagesPath("icon_return"),
          width: 8,
          height: 14,
        ),
      ),
    );
  }
}
